package com.sls.web.manager.action.recharge;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;

import com.sls.web.manager.action.SuperAction;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.common.WebTools;
import com.sls.web.manager.page.Pagination;
import com.sls.web.manager.recharge.pojo.Area;
import com.sls.web.manager.recharge.querybean.AreaQuery;
import com.sls.web.manager.recharge.service.AreaService;

/**
 * @author sxf
 * 
 */
public class AreaAction extends SuperAction {
	private static final long serialVersionUID = 2618748899029880727L;

	@Resource(name = "areaService")
	private AreaService areaService;

	private Pagination<Area> page;
	private AreaQuery areaQuery;

	public String ajaxShow() throws Exception {
		if (areaQuery == null) {
			areaQuery = new AreaQuery();
			areaQuery.setCurrentPage(Constants.INIT_CURRENTPAGE);
			areaQuery.setPageSize(Constants.INIT_PAGESIZE);
		}
		String gradePar = WebTools.getParameter("grade");
		if (StringUtils.isNotBlank(gradePar)) {
			try {
				areaQuery.setGrade(Integer.valueOf(gradePar));
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
		page = areaService.queryPage(areaQuery, getCurrentOperator());
		message = WebTools.writeAjax(page);
		return "ajax";
	}

	public String toShow() throws Exception {
		return "view";
	}

	public String toTest() throws Exception {
		return "test";
	}

	/**
	 * 加载地区(ajax)
	 * 
	 * @return
	 * @throws Exception
	 */
	public String loadArea() throws Exception {
		// 父区域代码
		String parentCode = WebTools.getParameter("parentCode");
		// 为1则查询省份
		String grade = WebTools.getParameter("grade");
		List<Area> areaList = null;
		AreaQuery query = new AreaQuery();
		if (StringUtils.isNotBlank(parentCode)) {
			query.setParentCode(parentCode);
		}
		if (StringUtils.isNotBlank(grade)) {
			try {
				query.setGrade(Integer.valueOf(grade));
			} catch (Exception e) {

			}
		}
		if (query.getGrade() == null) {
			query.setGrade(1);// 查省份
		}
		if ((query.getGrade() > 1 && query.getParentCode() != null)
				|| query.getGrade() == 1) {
			areaList = areaService.queryList(query, getCurrentOperator());
			message = WebTools.writeAjax(areaList);
		}
		return "ajax";
	}

	/**
	 * 加载地区(ajax分批次,为jquery.zTree树使用)
	 * 
	 * @return
	 * @throws Exception
	 */
	public String loadzTree() throws Exception {
		String parentCode = (String) WebTools.getParameter("parentCode");
		// 为1则查询省份
		String grade = (String) WebTools.getParameter("grade");
		List<Area> areaList = null;
		AreaQuery query = new AreaQuery();
		boolean flag = false;
		if (StringUtils.isNotBlank(parentCode)) {
			query.setParentCode(parentCode);
			flag = true;
		}
		if (StringUtils.isNotBlank(grade)) {
			try {
				query.setGrade(Integer.valueOf(grade));
				flag = true;
			} catch (Exception e) {
				query.setGrade(0);
			}
		}
		if (flag) {
			areaList = areaService.queryList(query, getCurrentOperator());
		}

		List<Map<String, String>> zTreeList = new ArrayList<Map<String, String>>();
		if (areaList != null && !areaList.isEmpty()) {
			for (Area area : areaList) {
				Map<String, String> map = new HashMap<String, String>();
				map.put("id", area.getCode());
				map.put("name", area.getAreaName());
				map.put("grade", Integer.toString(area.getGrade()));
				if (area.getGrade() < 3) {
					map.put("isParent", "true");
				}
				zTreeList.add(map);
			}
		}
		message = WebTools.writeAjax(zTreeList);
		return "ajax";
	}

	/**
	 * 加载地区(ajax全部,为jquery.zTree树使用)
	 * 
	 * @return
	 * @throws Exception
	 */
	public String loadAllDataZTree() throws Exception {
		List<Area> areaList = new LinkedList<Area>();
		areaList.addAll(areaService.queryAllList(1, getCurrentOperator()));
		areaList.addAll(areaService.queryAllList(2, getCurrentOperator()));
		List<Map<String, String>> zTreeList = new LinkedList<Map<String, String>>();
		if (areaList != null && !areaList.isEmpty()) {
			for (Area area : areaList) {
				Map<String, String> map = new HashMap<String, String>();
				map.put("id", area.getCode());
				map.put("pId", area.getParentCode());
				map.put("name", area.getAreaName());
				map.put("grade", Integer.toString(area.getGrade()));				
				if (area.getGrade() < 2) {
					map.put("isParent", "true");
				}
				zTreeList.add(map);
			}
		}
		message = WebTools.writeAjax(zTreeList);
		return "ajax";
	}

	public Pagination<Area> getPage() {
		return page;
	}

	public void setPage(Pagination<Area> page) {
		this.page = page;
	}

	public AreaQuery getAreaQuery() {
		return areaQuery;
	}

	public void setAreaQuery(AreaQuery areaQuery) {
		this.areaQuery = areaQuery;
	}

}
